{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# advancedClass"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " ## 1 class的另一种定义"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Writing advancedClass/define1.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile advancedClass/define1.py\n",
    "class Student(object):\n",
    "    name = 'Student'\n",
    "    def setName(self,newName):\n",
    "        self.name = newName\n",
    "s1 = Student()\n",
    "s2 = Student()\n",
    "s1.name = 's1'\n",
    "s2.name = 's2'\n",
    "print(s1.name,s2.name,Student.name)\n",
    "s1.setName('newS1')\n",
    "print(s1.name)\n",
    "print(Student.__dict__)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "注意几点：\n",
    "- 很多c++或java程序员最喜欢的方式。好像也可以定义实例属性，其实思路完全错误。\n",
    "- python中类是对象的模版这句话其实是错误的。\n",
    "- python中的class首先是一个对象，他有自己的属性和方法，其次，他才是一个实例对象的创建者。所以它具有双重的人格。\n",
    "- 自我人格的体现：类属性和类方法，一个特殊的类方法：__new__ \n",
    "- 模版的人格体现为：1 实例方法的定义 2 __init__ 的定义.\n",
    "- 其实后者是向传统面向对象语言的妥协，采用了语法蜜糖。如下面的例子：type函数的强大功能。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2 第一个语法蜜糖，躲在后面的type函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Writing advancedClass/abouttype.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile advancedClass/abouttype.py\n",
    "cs = type('Student',(),{'name':\"test1\",'age':10})\n",
    "s1 = cs()\n",
    "print(s1.name,s1.age)\n",
    "s2 = cs()\n",
    "print(s1.name,s1.age)\n",
    "s1.name = 's1'\n",
    "s2.name = 's2'\n",
    "print(s1.name,s2.name,cs.name)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3 第二个语法蜜糖，躲在后面的methodtype函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Writing advancedClass/aboutmtype.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile advancedClass/aboutmtype.py\n",
    "def setName(self,newName):\n",
    "    self.name = newName\n",
    "from types import MethodType\n",
    "cs.setName = MethodType(setName,cs)\n",
    "s3 = cs()\n",
    "s3.setName('S3')\n",
    "print(cs.name)\n",
    "\n",
    "print(getattr(s3,\"name\"))\n",
    "print(getattr(cs,\"name\"))\n",
    "print(getattr(cs,'setName'))\n",
    "print(getattr(s3,'setName'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import types\n",
    "dir(types)\n",
    "print(help(types))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
